<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function get_menu(){
		return $this->db->order_by('ordering')->get_where('menu',array('parent'=>0))->result();
	}
	
	public function get_menu_by_heading_id($id){
		return $this->db->order_by('ordering')->get_where('menu',array('heading_id'=>$id,'parent'=>0))->result();
	}
	
	public function get_child_menu($parent_id){
	//$this->db->select
	return $this->db->get_where('menu',array('parent'=>$parent_id))->result();
	}
	public function create_menu($insert)
	{
		//$this->db->insert('menu',$insert);
		//echo $this->db->last_query();
		if($this->db->insert('menu',$insert)){
			echo 'Menu Created Successfully';
		}
		else{
			echo 'Unable to create menu';
		}	
	}
	
	public function update_menu($insert,$edit_id){
		if($this->db->update('menu',$insert,array('id' => $edit_id ))){
			echo 'Menu Updated Successfully';
		}
		else{
			echo 'Unable to Update menu';
		}	
	}
	
	
	public function get_menu_heading(){
		return $this->db->order_by('ordering')->get('menu_heading')->result();
	}
	
	public function get_access(){
		return $this->db->get('menu_access')->result();
	}
	
	public function get_heading_access(){
		return $this->db->get('menu_heading_access')->result();
	}
	
	public function grant_access($menus)
	{
		$this->db->truncate('menu_access'); 
		foreach($menus as $key=>$value){
			// print($key);
			// print_r($value);
			foreach($value as $k=>$v){			
			$this->db->insert('menu_access',array('menu_id'=>$key,'group_id'=>$k));
			
			}
		}
	}
	
	public function grant_heading_access($heading_menus)
	{
		$this->db->truncate('menu_heading_access'); 
		foreach($heading_menus as $key=>$value){
			// print($key);
			// print_r($value);
			foreach($value as $k=>$v){			
			$this->db->insert('menu_heading_access',array('heading_id'=>$key,'group_id'=>$k));
			
			}
		}
	}
	/*Heading features*/
	public function create_heading($name,$icon)
	{
		//echo 'hrl;lop0';
		//exit;
		$insert = array(
		'name'=>$name,
		'icon'=>$icon,
		
		);	
		//	print_r($insert);
		//$this->db->_compile_insert(); 	
		$this->db->insert('menu_heading',$insert);
		//echo $this->db->last_query();
		
		//exit;
	}
	
	public function sorting($action,$updateRecordsArray)
		{
			
		if ($action == "updateRecordsListings"){
		
			$listingCounter = 1;
			foreach ($updateRecordsArray as $recordIDValue) {
				
				$this->db->update('menu_heading',array('ordering'=>$listingCounter),array('id'=>$recordIDValue));
		/*
				$query = "UPDATE records SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
				mysql_query($query) or die('Error, insert query failed');*/
				$listingCounter = $listingCounter + 1;
		
	//	echo  $recordIDValue;
			}
		
			//echo '<pre>';
			print_r($updateRecordsArray);
			//echo '</pre>';
			//echo 'If you refresh the page, you will see that records will stay just as you modified.';
		} 
		}
		
		
		public function getmenu_details($id_menu){
				$menus =  $this->db->get_where('menu',array('id' => $id_menu))->row();
				return $menus;
				//print_r($menus);exit;
			
		}
		
		public function menulist(){
			
			return $this->db->order_by('ordering')->get('menu')->result();
			
		}
		
		/*user access menus*/
		public function main_menu_sort($action,$updateRecordsArray)
		{
			
		if ($action == "updateRecordsListings"){
		
			$listingCounter = 1;
			foreach ($updateRecordsArray as $recordIDValue) {
				
				$this->db->update('menu',array('ordering'=>$listingCounter),array('id'=>$recordIDValue));
		/*
				$query = "UPDATE records SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
				mysql_query($query) or die('Error, insert query failed');*/
				$listingCounter = $listingCounter + 1;
		
	//	echo  $recordIDValue;
			}
		
			//echo '<pre>';
			print_r($updateRecordsArray);
			//echo '</pre>';
			//echo 'If you refresh the page, you will see that records will stay just as you modified.';
		} 
		}
		
		
	
}
